package picobot.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

/**
 * this class represents a menu bar for the PicobotUserInterface
 */
public class PicoMenuBar {

	/**
	 * the whole menu bar is created in this method using the
	 * PicobotUserInterface it is meant for
	 * 
	 * @param ui
	 *            is the PicobotUserInterface
	 * @return the complete menu bar as a JMenuBar
	 */
	public JMenuBar createPicoMenuBar(final PicobotUserInterface ui) {
		JMenuBar menuBar;
		JMenu simulation, run;
		JMenuItem menuItem;

		menuBar = new JMenuBar();

		// defining the simulation menu
		simulation = new JMenu("Simulation");
		simulation.setMnemonic(KeyEvent.VK_S);
		simulation.getAccessibleContext().setAccessibleDescription(
				"The menu for simulations");
		menuBar.add(simulation);

		// adding the two menu items, 'load map' and 'load rules' to the
		// simulation menu

		// the load map menu item, it uses a LoadMapAction
		menuItem = new JMenuItem("Load Map", KeyEvent.VK_M);
		menuItem.getAccessibleContext().setAccessibleDescription(
				"This will let you load a map");
		menuItem.addActionListener(new LoadMapAction(ui));
		simulation.add(menuItem);

		// the load rules menu item, it uses a LoadRuleAction
		menuItem = new JMenuItem("Load Rules", KeyEvent.VK_U);
		menuItem.getAccessibleContext().setAccessibleDescription(
				"This will let you load rules ");
		menuItem.addActionListener(new LoadRuleAction(ui));
		simulation.add(menuItem);

		// the change skin menu item, it uses a ChangeSkinAction
		menuItem = new JMenuItem("Change Skin", KeyEvent.VK_C);
		menuItem.getAccessibleContext().setAccessibleDescription(
				"This will let you change the skins");
		menuItem.addActionListener(new LoadSkinAction(ui));
		simulation.add(menuItem);

		// defining the run menu
		run = new JMenu("Run");
		run.setMnemonic(KeyEvent.VK_R);
		run.getAccessibleContext().setAccessibleDescription(
				"The menu to run a simulation");
		menuBar.add(run);

		// adding the four menu items, 'step', 'go', 'stop' and 'reset' to the
		// run menu

		// the step menu item, it uses a StepAction
		menuItem = new JMenuItem("Step", KeyEvent.VK_T);
		menuItem.getAccessibleContext().setAccessibleDescription(
				"This will let you take a step");
		menuItem.addActionListener(new StepAction(ui));
		run.add(menuItem);

		// the go menu item, it uses a GoAction
		menuItem = new JMenuItem("Go", KeyEvent.VK_G);
		menuItem.getAccessibleContext().setAccessibleDescription(
				"This will start a run");
		final GoAction goAction = new GoAction(ui);
		menuItem.addActionListener(goAction);
		run.add(menuItem);

		// the stop menu item, it performs the taskCancel method of the
		// GoAction, which is implemented by the go menu item above
		menuItem = new JMenuItem("Stop", KeyEvent.VK_O);
		menuItem.getAccessibleContext().setAccessibleDescription(
				"This will stop a run");
		menuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				goAction.taskCancel();
			}
		});
		run.add(menuItem);

		// the reset menu item, it performs the reset method of the simulator of
		// the PicobotUserInterface and the panelReset method of the PicoPanel
		// of the PicobotUserInterface
		menuItem = new JMenuItem("Reset", KeyEvent.VK_E);
		menuItem.getAccessibleContext().setAccessibleDescription(
				"This reset a simulation");
		menuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ui.getSimulator().reset();
				ui.getScreen().panelReset();
			}
		});
		run.add(menuItem);

		// returns the created menu bar
		return menuBar;

	}
}
